package ui
{
	import com.bit101.components.Text;
	
	import flash.display.Graphics;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.filters.GlowFilter;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.utils.getTimer;
	
	import org.musince.util.TextFieldUtil;
	
	import utils.UIUtil;
	
	public class OptionDialog extends Dialog
	{
		private var _root:Sprite = new Sprite;
		private var _bg:Shape = new Shape;
		private var _quest:TextField = new TextField();
		private var _optionLayer:Sprite;
		private var _optionInterval:int = 80;
		private var _options:Vector.<TextField>;
		private var _splitMargin:int = 30;
		private var _hits:Vector.<Rectangle>;
		private var _selectWidth:int = 160;
		private var _selectHeight:int = 36;
		private var _selectMask:Shape;
		private var _selecting:int;
		private var _selectFilters:Array = [new GlowFilter(0xFFE397, 1, 2, 2)];
		private var _selectFormat:TextFormat;
		private var _unselectFormat:TextFormat;
		
		public var onSelect:Function;
		
		public function OptionDialog()
		{
			super();		
			_quest = TextFieldUtil.getUIText(36, 0xF9E987);
			_quest.x = _optionInterval;
			_quest.y = 20;
			_optionLayer = new Sprite();
			_optionLayer.y = 100;
			_optionLayer.mouseChildren = false;
			_optionLayer.mouseEnabled = false;
			_selectMask = UIUtil.drawSelect(_selectWidth, _selectHeight, 0x9B6C34);
			_selectMask.y = _optionLayer.y + 4;			
			_root.addChild(_bg);
			_root.addChild(_quest);
			_root.addChild(_selectMask);
			_root.addChild(_optionLayer);			
			this.addChild(_root);
		}
		
		override public function onActive():void {
			_root.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
			_root.addEventListener(MouseEvent.CLICK, onMouseClick);
		}
		
		override public function onUnActive():void {
			_root.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
			_root.removeEventListener(MouseEvent.CLICK, onMouseClick);
		}
		
		public function setContent(text:String, option:Array):void {
			_quest.text = text;
			_options = new Vector.<TextField>(option.length);
			_hits = new Vector.<Rectangle>(option.length);
			_optionLayer.removeChildren();
			var tf:TextField;
			for (var i:int = 0; i < option.length; i++) {
				tf = _options[i] = TextFieldUtil.getUIText(28, 0x968980);
				tf.text = option[i];
				_hits[i] = new Rectangle(0, 0, tf.width, tf.height);
				_optionLayer.addChild(tf);
			}
			var prev:TextField; 
			for (i = 1; i < option.length; i++) {
				prev = _options[i-1];
				tf = _options[i];
				tf.x = prev.x + prev.width + _optionInterval;
				_hits[i].x = tf.x;
			}
			var ow:int = tf.x + tf.width;
			var w:int = Math.max(_quest.width, ow);
			w += _optionInterval * 2;
			var g:Graphics = _bg.graphics;
			g.clear();
			g.beginFill(0x191919, 0.9);
			g.drawRoundRect(0, 0, w, 160, 20, 20);
			g.endFill();
			g.lineStyle(2, 0x64583E); 
			g.moveTo(_splitMargin, 88);
			g.lineTo(w-_splitMargin, 88);
			_optionLayer.x = (w - ow) / 2;
			_quest.x = (w - _quest.width) / 2;
//			g.beginFill(0xFF0000);
			for (i = 0; i < option.length; i++) {
				_hits[i].x += _optionLayer.x;
				_hits[i].y += _optionLayer.y;
//				g.drawRect(_hits[i].x, _hits[i].y, _hits[i].width, _hits[i].height); 
			}
			_unselectFormat = _options[0].defaultTextFormat;
			_selectFormat = _options[0].defaultTextFormat;
			_selectFormat.color = 0xFFF3ED;
			select(0);
			var bound:Rectangle = _root.getBounds(_root);
			_root.x = -_root.width/2;
			_root.y = -_root.height/2;
		}
		
		private function select(i:int):void {
			_selecting = i;
			_selectMask.width = _options[i].width + _optionInterval;
			_selectMask.x = _optionLayer.x + _options[i].x + _options[i].width/2 - _selectMask.width/2;
			for (var j:int = 0; j < _options.length; j++) {
				_options[j].filters = null;
				_options[j].setTextFormat(_unselectFormat);
			}			
			_options[i].filters = _selectFilters;
			_options[i].setTextFormat(_selectFormat);
			
//			var rt:Rectangle = _selectMask.getBounds(_root);
//			trace(rt);
		}
		
		private function onMouseMove(e:MouseEvent):void {
			var x:int = _root.mouseX;
			var y:int = _root.mouseY;
//			trace("mouse move x:" + x + " y:" + y);
			for (var i:int = 0; i < _hits.length; i++) {
				if (_hits[i].contains(x, y) && _selecting != i) {
					select(i);
				}
			}
		}
		
		private function onMouseClick(e:MouseEvent):void {
			if (onSelect == null) return;
			var x:int = _root.mouseX;
			var y:int = _root.mouseY;			
			if (_selectMask.getBounds(_root).contains(x, y)) {
				onSelect(_selecting);
			}
//			for (var i:int = 0; i < _hits.length; i++) {
//				if (_hits[i].contains(x, y)) {
//					onSelect(i);
//					break;
//				}
//			}			
		}
	}
}